#!/bin/bash
# 一键构建+启动镜像脚本
# can used in centos
set -e

custom_from_file="custom_from.txt"
# 选择基础镜像
dockerfile_from() {
	local title_box="基础镜像列表，使用空格选定"
	local check_box="选择基础镜像:"
	local check_options=(
		"ubuntu:20.04" "	ubuntu:20.04"
		"openjdk:8" "	openjdk:8"
		"php:7.4-fpm" "	php:7.4-fpm"
		other "	其他自定义镜像"
	)
	local choice=($(
		whiptail --fullbuttons --separate-output --title "${title_box}" \
			--menu "${check_box}" 0 0 0 "${check_options[@]}" 3>&1 1>&2 2>&3
	))
	from_choosed="${choice[@]}"
}

# 选择自定义基础镜像
custom_from() {
	if [ ! -f "$custom_from_file" ]; then
		echo "错误：选择了使用[自定义镜像]，但未定义“自定义镜像文件”：custom_from.txt"
		exit 1
	fi
	
	local title_box="自定义基础镜像列表，使用空格选定"
	local check_box="选择自定义基础镜像:"
	local check_options=()
	
	while IFS= read -r line; do
		check_options+=("$line" "")
	done < "$custom_from_file"

	local choice=($(
		whiptail --fullbuttons --separate-output --title "${title_box}" \
			--menu "${check_box}" 0 0 0 "${check_options[@]}" 3>&1 1>&2 2>&3
	))
	from_choosed="${choice[@]}"
}

# 是否需要拷贝资源文件
copy_need() {
	echo "是否需要拷贝资源文件? (y/n, 默认n)"
	read copy_need_txt
	if [ -z "$copy_need_txt" ]; then
		copy_need_txt="n"
	fi
	if [ "${copy_need_txt}" == "y" ] || [ "${copy_need_txt}" == "Y" ]; then
		copy_url
	fi
}

# 复制资源
copy_url() {
	local pwd=$(pwd)
	echo "请输入资源路径(宿主机绝对路径，默认为当前路径:$pwd):"
	read input_text
	if [ -z "$input_text" ]; then
		input_text="."
	fi
	
	echo "请输入目标路径(容器内绝对路径，默认:$dist_dir):"
	read input_text2
	if [ -z "$input_text2" ]; then
		input_text2="$dist_dir"
	fi
	echo "COPY $input_text $input_text2" >> Dockerfile
	
	echo "需要继续拷贝？（y/n，默认n）"
	read continue_text
	if [ -z "$input_text" ]; then
		continue_text="n"
	fi
	if [ "${continue_text}" == "y" ] || [ "${continue_text}" == "Y" ]; then
		copy_url
	fi
}

# 写入启动命令
cmd_command() {
	echo "请输入容器内服务启动/执行命令:"
	read input_text3
	if [ "${input_text3}" == "" ]; then
		echo "NOT CMD"
	else
		echo "ENV COMMAND=\"$input_text3\"" >> Dockerfile
		echo "CMD [\$COMMAND]" >> Dockerfile
	fi
}

# 获取系统信息
get_os() {
	if [ -f /etc/os-release ]; then
		. /etc/os-release

		# 根据 ID 和 ID_LIKE 字段来判断操作系统
		os_name="$ID"
	else
		echo "无法确定操作系统"
	fi
}

# 获取输入端口
expose() {
	echo "请输入需要开放的端口：(默认80,多个使用 空格 隔开)"
	read expose_port
	if [ -z "$expose_port" ]; then
		expose_port=80
	fi
	
	expose_ports=""
	OLD_IFS="$IFS"
	IFS=' '
	read -r -a ports <<< "$expose_port"
	for port in "${ports[@]}"; do
		expose_ports+=" -p $port:$port "
	done
	IFS="$OLD_IFS"
}

# 获取输入镜像名称
image_name() {
	echo "请输入镜像名称："
	read img_name
	if [ -z "$img_name" ]; then
		image_name
	fi
}

# 主程序
main() {
	# 选择基础镜像
	dockerfile_from
	dist_dir="/app"
	if [ "${from_choosed}" == "other" ]; then
		custom_from
	fi
	if [[ "${from_choosed}" == *"php"* ]]; then
		dist_dir="/var/www/html"
	fi
	if [ "${from_choosed}" == "" ]; then
		exit 1
	fi
	
	# 生成Dockerfile文件
	echo "选择了基础镜像：${from_choosed}"
	echo "创建Dockerfile..."
	touch Dockerfile
	echo "FROM ${from_choosed}" > Dockerfile
	echo "ENV TZ=Asia/Shanghai" >> Dockerfile
	echo "RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime" >> Dockerfile
	echo "RUN echo 'Asia/Shanghai' >/etc/timezone" >> Dockerfile
	echo "WORKDIR $dist_dir" >> Dockerfile
	copy_need
	expose
	echo "EXPOSE $expose_port" >> Dockerfile
	cmd_command
	echo "####生成的Dockerfile文件内容为："
	cat Dockerfile
	echo ""
	image_name
	
	# 构建、启动镜像
	echo "构建镜像..."
	docker build -t "$img_name" .
	echo "启动镜像.."
	# --memory=1048m --restart always
	echo "docker run --name $img_name -d $expose_ports $img_name"
	docker run --name "$img_name" -d $expose_ports "$img_name"
	echo "启动完成."
}

main